package cn.ljy.uem.service.impl;

import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolClassQuery;
import cn.ljy.uem.model.query.SchoolStudentQuery;
import cn.ljy.uem.service.SchoolClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;


/**
 * @description 行政班模块的业务层接口
 * @author廖振豪
 * @date 2022-7-6 10:45
 */
@Service
@Slf4j
public class ISchoolClassService extends IBaseService<SchoolClass, SchoolClassQuery> implements SchoolClassService {

    @Autowired
    private SchoolClassMapper mapper;

    @Autowired
    private SchoolMajorMapper schoolMajorMapper;

    @Autowired
    private SchoolGradeMapper schoolGradeMapper;

    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;
    @Override
    protected void beforeGet(Serializable id) {
    }

    @Override
    protected void afterGet(SchoolClass schoolClass) {
        if(schoolClass.getMajorId()!=null){
            SchoolMajor schoolMajor= schoolMajorMapper.findById(schoolClass.getMajorId());
            schoolClass.setSchoolMajor(schoolMajor);
        }
        if(schoolClass.getGradeId()!=null){
            SchoolGrade schoolGrade= schoolGradeMapper.findById(schoolClass.getGradeId());
            schoolClass.setSchoolGrade(schoolGrade);
        }
        if(schoolClass.getTeacherId()!=null){
            SchoolTeacher schoolTeacher= schoolTeacherMapper.findById(schoolClass.getTeacherId());
            schoolClass.setSchoolTeacher(schoolTeacher);
        }
    }

    @Override
    protected void beforeUpdate(SchoolClass schoolClass) {

    }

    @Override
    protected void beforeSave(SchoolClass schoolClass) {

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
       //获取要删除的行政班id
        for (Serializable id:ids){
             //通过班级id 查找该班级下学生是否为空
            SchoolStudentQuery schoolStudentQuery=new SchoolStudentQuery();
            schoolStudentQuery.setClassId((Integer) id);
            //通过学生的查找类查找
           if(schoolStudentMapper.findAll(schoolStudentQuery).size()>0){
               throw new MyWebException("删除失败：该班级下存在学生");
           }
//            System.out.println("学生人数");
//            System.out.println(schoolStudentMapper.findAll(schoolStudentQuery).size());
        }
    }

    @Override
    protected void beforeFindAll(SchoolClassQuery query) {
        if(query!=null){
            //如果传递了学校部门
            if(query.getDepartmentId()!=null){
                //获取专业集合id
                query.setMajorIds(schoolMajorMapper.findIdByDepartmentId(query.getDepartmentId()));
            }
        }
    }

    //行政班多表查询
    @Override
    protected void afterFindAll(List<SchoolClass> list) {
        for (SchoolClass schoolClass:list){
            if(schoolClass.getMajorId()!=null){
                SchoolMajor schoolMajor= schoolMajorMapper.findById(schoolClass.getMajorId());
                schoolClass.setSchoolMajor(schoolMajor);
            }
            if(schoolClass.getGradeId()!=null){
                SchoolGrade schoolGrade= schoolGradeMapper.findById(schoolClass.getGradeId());
                schoolClass.setSchoolGrade(schoolGrade);
            }
            if(schoolClass.getTeacherId()!=null){
                SchoolTeacher schoolTeacher= schoolTeacherMapper.findById(schoolClass.getTeacherId());
                schoolClass.setSchoolTeacher(schoolTeacher);
            }
        }
        super.afterFindAll(list);
    }

}
